package com.ocom.accesservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.accesservice.dao.IDaoPersonnelYkm;
import com.ocom.accesservice.service.PersonnelYkmService;
import com.ocom.accesservice.vo.ParaSumVo;
import com.ocom.accesservice.vo.YkmSumListVo;
import com.ocom.accesservice.vo.devicelogin.GetYkmPersonnelListVo;
import com.ocom.accesservice.vo.devicelogin.YkmSumListRVo;
import com.ocom.common.entity.access.EntityPersonnelYkm;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.AES;
import com.ocom.common.utils.MD5;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PersonnelYkmServiceImpl extends ServiceImpl<IDaoPersonnelYkm, EntityPersonnelYkm> implements PersonnelYkmService {

    @Autowired
    private IDaoPersonnelYkm iDaoPersonnelYkm;

    //根据人员信息的comId+pId查询防疫监控信息表personnel_ykm
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public EntityPersonnelYkm GetPersonnelYkm(Long comId, Long pId) {
        QueryWrapper<EntityPersonnelYkm> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("p_id",pId);
        return iDaoPersonnelYkm.selectOne(query);
    }

    /**
     * 防疫人员列表（getYkmPersonnelList）
     *
     * @return*/
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<GetYkmPersonnelListVo> GetYkmPersonnelList(Long comId,List<Integer> deptIdsList,String pCode,
                                                           String pName,Integer codeColor,Integer hsResult) {

        return   iDaoPersonnelYkm.GetYkmPersonnelList(comId,deptIdsList,pCode,pName,codeColor,hsResult);

    }
    /**
     * 防疫汇总指标列表详情（getYkmSumList）
     *
     * @return*/
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<YkmSumListRVo> getYkmSumList(Long comId, YkmSumListVo ykmSumListVo) {
        List<Integer> deptIdsList = new ArrayList<>();
        if (ykmSumListVo.getDeptIds() != null && !ykmSumListVo.getDeptIds().isEmpty()){
            String[] deptIdsArr = ykmSumListVo.getDeptIds().split(",");
            for (String deptId: deptIdsArr) {
                if (deptId.isEmpty()){
                    continue;
                }
                deptIdsList.add(Integer.valueOf(deptId));
            }
        }
        ParaSumVo paraSumVo = new ParaSumVo();
        if (ykmSumListVo.getParaSum() != null && !ykmSumListVo.getParaSum().isEmpty()){
            String[] paraSumArr = ykmSumListVo.getParaSum().split(",");
            for (String paraSum: paraSumArr) {
                if (paraSum.equals("green")){
                    paraSumVo.setGreen(paraSum);
                }
                if (paraSum.equals("pullFailed")){
                    paraSumVo.setPullFailed(paraSum);
                }
                if (paraSum.equals("other")){
                    paraSumVo.setOther(paraSum);
                }
                if (paraSum.equals("red")){
                    paraSumVo.setRed(paraSum);
                }
                if (paraSum.equals("yellow")){
                    paraSumVo.setYellow(paraSum);
                }
                if (paraSum.equals("noNucleic")){
                    paraSumVo.setNoNucleic(paraSum);
                }
                if (paraSum.equals("positive")){
                    paraSumVo.setRed(paraSum);
                }
                if (paraSum.equals("negative")){
                    paraSumVo.setRed(paraSum);
                }
                if (paraSum.equals("hours24")){
                    paraSumVo.setHours24(paraSum);
                }
                if (paraSum.equals("hours48")){
                    paraSumVo.setHours48(paraSum);
                }
                if (paraSum.equals("hours72")){
                    paraSumVo.setHours72(paraSum);
                }
                if (paraSum.equals("within7Days")){
                    paraSumVo.setWithin7Days(paraSum);
                }
                if (paraSum.equals("more7Days")){
                    paraSumVo.setMore7Days(paraSum);
                }
                if (paraSum.equals("ym0")){
                    paraSumVo.setYm0(paraSum);
                }
                if (paraSum.equals("ym1")){
                    paraSumVo.setYm1(paraSum);
                }
                if (paraSum.equals("ym2")){
                    paraSumVo.setYm2(paraSum);
                }
                if (paraSum.equals("ym3")){
                    paraSumVo.setYm3(paraSum);
                }
                if (paraSum.equals("ymMore3")){
                    paraSumVo.setYmMore3(paraSum);
                }
            }
        }
        String key = MD5.MD5Encode(ykmSumListVo.getComId().toString(), "utf8");
        String encryIdentity = null;
        if (StringUtils.isNotEmpty(ykmSumListVo.getIdentity())){
            encryIdentity = AES.encrypt(ykmSumListVo.getIdentity(),key);
        }
        String encryPhone = null;
        if (StringUtils.isNotEmpty(ykmSumListVo.getPhone())){
            encryPhone = AES.encrypt(ykmSumListVo.getPhone(),key);
        }


        return  iDaoPersonnelYkm.getYkmSumList(ykmSumListVo.getComId(),deptIdsList,
                ykmSumListVo.getPCode(), ykmSumListVo.getPName(),
                 paraSumVo.getPullFailed(),paraSumVo.getOther(),paraSumVo.getRed(),
                paraSumVo.getYellow(),paraSumVo.getGreen(),paraSumVo.getPositive(),
                paraSumVo.getNegative(),paraSumVo.getNoNucleic(),
                paraSumVo.getHours24(),paraSumVo.getHours48(),paraSumVo.getHours72(),paraSumVo.getWithin7Days(),
                paraSumVo.getMore7Days(),paraSumVo.getYm0(),paraSumVo.getYm1(),paraSumVo.getYm2(),
                paraSumVo.getYm3(),paraSumVo.getYmMore3(),
                encryIdentity,encryPhone );
    }


}

